package com.kapcb.common.result;

import com.kapcb.common.request.BasePage;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * <a>Title: PageResult </a>
 * <a>Author: cb <a>
 * <a>Description: PageResult <a>
 *
 * @author cb
 * @version 1.0
 * @date 2024/4/19 17:02
 * @since 1.0
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageResult<T> implements Serializable {

    private static final long serialVersionUID = -3473669660048437478L;

    /**
     * 当前页码
     */
    private long pageNum;

    /**
     * 分页条数
     */
    private long pageSize;

    /**
     * 总数
     */
    private long total;

    /**
     * 分页数据
     */
    private List<T> items;

    public static <T> PageResult<T> emptyResult(BasePage basePage) {
        return new PageResult<>(new ArrayList<>(), 0L, basePage);
    }

    public PageResult(List<T> items, Long total, BasePage basePage) {
        this.pageNum = basePage.getPageNum();
        this.pageSize = basePage.getPageSize();
        if (CollectionUtils.isEmpty(items)) {
            this.total = 0L;
            this.items = new ArrayList<>();
        } else {
            this.total = total;
            this.items = items;
        }
    }

}
